//表格数据实例初始化查询条件
var field='QQ,QL,';
var field_init="'QQ'"+",'QL'";
var sdate='2014-05-07';
var edate='2014-07-16';
var node='延1评3井,';
//查询参数
var s_date="";//开始时间
var e_date="";//结束时间
var nodeStrs = "";//tree选中的节点
var fieldStrs = "";//checkboxs数据选中的Obj



var headers={"SCSJ":"生产时间（h）","BJBS":"泵径/泵深（m ）",
		"CCCC":"冲次/冲程（m/min）",
		"TL":"电流（A）","TY":"套压（Mpa）",
		"DYM":"动液面（m）","YJYM":"日降液面（m）",
		"PWF":"井底流压（Mpa）","DPWF":"日降井底流压（Mpa）",
		"MFJK":"煤粉情况","BX":"泵效（%）",
		"QQ":"日产气（m3）","QL":"日产液（m3）",
		"CL":"氯根（mg/L）","PH":"PH值",
		"RQYQ":"燃气机用气（m3/d）","BAK":"备注",
		"YLCQ":"月累产气（m3）","LLCQ":"年累产气（m3）",
		"LJCQ":"累产气量（m3）","YLCY":"月累产液（m3）",
		"LLCY":"年累产液（m3）","LJCY":"累产液量（m3）"};

Ext.onReady(function(){
	panelcontain();//right-down面板初始化
	tools();       //日期控件和button控件
	//headerHandler();
});


//header转化
var headerHandler=function(field){
	var label=headers[field];
	return label;
}


//实例表格数据store
var dataStore=Ext.create('Ext.data.Store', {
    storeId:'simpsonsStore1',
    fields:eval("["+"'WELLNAME'"+",'SCRQ'"+","+field_init+"]"),
    proxy: {
        type: 'ajax',
        url:'./table/showTable?nodeStr='+encodeURI(encodeURI(node))+'&fieldStr='+field+'&s_date='+sdate+'&e_date='+edate,
        reader: {
            type: 'json'
        }
    },
    autoLoad:true
});



//button click处理
var b_click=function (obj,e){
	//alert(obj.text);
	//alert(treeObj);
	if(obj.text=='查询'){
		getTableJson();
	}
	if(obj.text=='导出数据'){
		exportExcel();
	}
	
}

//条件参数获取
var getConPars=function(){
	s_date="";//开始时间
	e_date="";//结束时间
	nodeStrs = "";//tree选中的节点
	fieldStrs = "";//checkboxs数据选中的Obj

	//tree选中的节点
	var nodes = treeObj.getCheckedNodes(true);
	for (var i = 0; i < nodes.length; i++) {
		if (!nodes[i].isParent)
			nodeStrs += nodes[i].id + ",";// 获取选中节点的值，并添加到数组中
	}
	//checkboxs数据选中的Obj
	$("#left-property-table tr td").each(function() {
		if ($(this).find("input[type=checkbox]:checked").val() != undefined) {
			fieldCode = $(this).find("input[type=checkbox]:checked").val();
			fieldStrs += fieldCode + ",";
		}
	});
	//开始时间
	s_date=Ext.getCmp("from_date").getValue();
	s_date=Ext.util.Format.date(s_date,'Y-m-d');
	//结束时间
	e_date=Ext.getCmp("to_date").getValue();
	e_date=Ext.util.Format.date(e_date,'Y-m-d');
}



//获取表格数据json
var getTableJson=function(){
	
	getConPars();

	var flag=conditionTest(nodeStrs,fieldStrs,s_date,e_date);//数据筛选条件验证
	if(flag==true){
		getJson(nodeStrs,fieldStrs,s_date,e_date); //获取Json数据
	}
	
	
}
//数据筛选条件验证
var conditionTest=function(nStr,fStr,sStr,eStr){
	if(nStr==""){
		alert("油气井为空！");
		return false;
	}
	if(fStr==""){
		alert("指标参数为空！");
		return false;
	}
	if(sStr==""){
		alert("开始日期为空！");
		return false;
	}
	if(sStr>=eStr){
		alert("日期选择错误！");
		return false;
	}
	return true;
}

//获取Json数据
var getJson=function(nStr,fStr,sStr,eStr){
	//查询
	Ext.data.StoreManager.clear();
	field=fStr;
	sdate=sStr;
	edate=eStr;
	field_init=field.substring(0,field.length-1);
	
	var objss="['WELLNAME','SCRQ',";
	var fieldinit=field_init.split(",");
	for(var i=0;i<fieldinit.length;i++){
		objss=objss+"'"+fieldinit[i]+"',"
	}
	objss=objss.substring(0,objss.length-1);
	objss=objss+"]";
	
	var objs=eval(objss);
	//dataStore.reload();
	Ext.create('Ext.data.Store', {
	    storeId:'simpsonsStore',
	    fields:objs,
	    proxy: {
	        type: 'ajax',
	        url:'./table/showTable?nodeStr='+encodeURI(encodeURI(nStr))+'&fieldStr='+field+'&s_date='+sdate+'&e_date='+edate,
	        reader: {
	            type: 'json'
	        }
	    },
	    autoLoad:true
	});
	
	var cols="[{header:"+"'行号'"+",xtype:"+"'rownumberer',"+"width:40"+"},{ header:"+"'井名称'"+",  dataIndex: "+"'WELLNAME'"+" },"
	      +"{ header: "+"'生产日期'"+", dataIndex:"+ "'SCRQ'"+"},";
	
	
	var flds=fStr.split(",");
	for(var i=0;i<flds.length-1;i++){
		cols=cols+"{ header: '"+headerHandler(flds[i])+"',  dataIndex: '"+flds[i]+"'},"
	}
	cols=cols.substring(0,cols.length-1);
	cols=cols+"]";
	
	var objcols = eval(cols); 
	
	var title=nStr.substring(0,nStr.length-1);
	var titles=title.split(",");
	if(titles.length>=3){
		title=titles[0]+","+titles[1]+"等"
	}
    var	gridObj1=Ext.create('Ext.grid.Panel', {
	    title: title+'-查询结果',
	    closable:true,
	    store: Ext.data.StoreManager.lookup('simpsonsStore'),
	    columns:objcols 
	});

Ext.getCmp('panels').add(gridObj1).show();

} 

//表格panel
var gridObj=Ext.create('Ext.grid.Panel', {
    title: '延1评3井-查询结果',
    closable:true,
    layout: {
        align: 'left'
    },
    store: Ext.data.StoreManager.lookup('simpsonsStore1'),
    columns: 
    	[{header: '行号',xtype: 'rownumberer',width: 40,sortable: false},
         { header: '井名称',  dataIndex: 'WELLNAME'},
         { header: '生产日期', dataIndex: 'SCRQ'},
         { header: '日产气', dataIndex: 'QQ' },
         { header: '日产液', dataIndex: 'QL' }
        ]
});



//right—down面板
var panelcontain=function (){
	
	Ext.create('Ext.tab.Panel', {
		id:'panels',
		deferredRender:false,
	    height: 420,
	    activeTab: 0,
	    items: [gridObj],
	    renderTo: Ext.get("table")
	});
	
}


//时间和buttons组合tools
var tools=function (){
	Ext.create('Ext.Panel', {
	deferredRender:false,
	border:0,
	layout:'hbox',
	items:[time,buttons],
    renderTo: Ext.get("tools")
});
}

//时间段选择
var time=Ext.create('Ext.Panel', {
//    renderTo: Ext.get("time"),
    width: 710,
    border:0,
    layout: 'hbox',
    padding: 4,
    items: [{
        xtype: 'datefield',
        id:'from_date',
        fieldLabel: '从',
        labelWidth:17,
        width:135,
        name: 'from_date',
        maxValue: new Date()// limited to the current date or prior
    }, {
        xtype: 'datefield',
        id:'to_date',
        fieldLabel: '到',
        labelWidth:17,
        width:135,
        name: 'to_date',
        value: new Date()  // defaults to today
    }]
});

//buttons工具
var buttons=Ext.create('Ext.Panel', {
//	renderTo: Ext.get("buttons"),
    width:500,
    border:0,
    padding: 4,
    items: [{
            xtype: 'button',
            text : '查询',
            width:80,
            handler:function(obj,e){
            	b_click(obj,e);
            }
        },
        {
            xtype: 'button',
            text : '导出数据',
            width:80,
            handler:function(obj,e){
            	b_click(obj,e);
            }
        }
    ]
});



